
Handlers =
{
	totalSize: 0,
	totalFiles: 0,
	uploaded: 0,
	currentState: 0,
	
	fileQueued : function( file ) {
		Handlers.totalSize += parseInt( file.size );
		Handlers.totalFiles = this.getStats().files_queued;
	},
	
	
	fileDialogStart : function()
	{
		Handlers.totalFiles = 0;
		Handlers.totalSize = 0;
		Handlers.uploaded = 0;
		Handlers.currentState = 0;		
	},
	
	fileDialogComplete: function() {

		this.startUpload();
	},
	
	uploadStart :  function( file ) {
	},
	
	uploadProgress : function( file, bytesLoaded, bytesTotal ) {
		Handlers.currentState = Handlers.uploaded + bytesLoaded;
		Handlers.refreshState.call( this, file );
	},
	
	uploadSuccess : function( file, serverData ) {
		var li = document.createElement( 'LI' );
		li.className = "jw_gallery_image";
		var html = "<table>" + serverData + "</table>";
		this.customSettings.cont.appendChild( li );
		li.innerHTML = html;
		Sortable.create( li.parentNode, { overlap: 'horizontal', constraint: '', onChange: jwGallery.EventHandlers.onChange } );
		jwGallery.EventHandlers.onChange( li );
	},
	
	uploadComplete : function( file ) {
		Handlers.currentState = Handlers.uploaded += 1.1 * parseInt( file.size );
		Handlers.refreshState.call( this, file );
	},
	
	refreshState: function( file )
	{
		var progress = this.customSettings.progress;
		var progressData = this.customSettings.progressData;
		progress.style.width = Math.round( Handlers.currentState / ( Handlers.totalSize * 1.1 ) * 200 ) + "px" ;
		var stats = Math.round( Handlers.currentState / ( 1.1 * Handlers.totalSize ) * 100 ) + "%";
		stats += "(" + Handlers.currentState.toString().parseAsSize() + "/" + Handlers.totalSize.toString().parseAsSize() + ")";
		progressData.innerHTML = stats;
	}
	
}

String.prototype.parseAsSize = function()
{
	var num = parseInt( this );
	
	pow = 0;
	
	while ( num > 1024 )
	{
		num = num / 1024;
		pow++;
	}
	
	switch( pow )
	{
		case 0:
			suffix = "b";
		break;

		case 1:
			suffix = "Kb";
		break;
		
		case 2:
			suffix = "Mb";
		break;
	
		case 3:
			suffix = "Gb";
		break;
	
		case 4:
			suffix = "Tb";
		break;
	}
	
	return Math.round( num * 100 ) / 100 + suffix;
	
}

jwGallery = {
	EventHandlers : {
		doDelete : function( element)
		{
			while( element.tagName != "LI" && element.className != "jw_gallery_image" )
			{
				element = element.parentNode;
			}
			
			var inputs = element.getElementsByTagName( 'INPUT' );
			
			element.parentNode.removeChild( element );
			
			// to remove double placed hidden fields...
			for( var i = 0; i < inputs.length; i++ )
			{
				if ( tmp = document.getElementById( inputs[i].id ) )
				{
					tmp.parentNode.removeChild( tmp );
				}
			}
		},
		onChange : function( _param )
		{
			var list = _param.parentNode.getElementsByTagName( 'LI' );
			for ( var i = 0; i < list.length; i++ )
			{
			  var inputs = list[i].getElementsByTagName( 'INPUT' );
			  inputs[inputs.length - 2 ].value = i - -1;
			}
		}		
	},
	DomManipulators : {
		getTableImagesContainer : function( element )
		{
			root = null;
			do
			{
				var list = element.getElementsByTagName( 'UL' );
				for ( var i = 0; i < list.length; i++ )
				{
					if ( list[i].className == 'jw_gallery_image_container' )
					{
						root = list[i];
						break;
					}
				}
				element = element.parentNode;
			}while( root == null )
			
			if ( !root.id )
				root.id = new Date().toUTCString() + parseInt( Math.random() * 10000 );
			return root;
		}		
	}
}

